#!/bin/bash

echo "==============================================="
echo "Post Post-Install Script"
echo "==============================================="

#echo "DEBUG: $ 1 = Full path to the installation package the installer app is processing: " $1
#echo "DEBUG: $ 2 = Full path to the installation destination: " $2
#echo "DEBUG: $ 3 = Installation volume (mountpoint) to receive the payload: " $3
#echo "DEBUG: $ 4 = Root directory for the system: " $4

echo "preinstall: Path to installer....... $1"
echo "preinstall: Path to destination..... $2"
echo "preinstall: Path to dest volume..... $3"
echo "preinstall: Root of system folder... $4"

#############################################################################

DEST_VOL="$3"
EFI_ROOT_DIR=$(cd "${DEST_VOL}"/Private/tmp/EFIROOTDIR; pwd -P)
CLOVER_INSTALLER_PLIST_NEW="${DEST_VOL}@CLOVER_INSTALLER_PLIST_NEW@"
CLOVER_INSTALLER_PLIST="${DEST_VOL}@CLOVER_INSTALLER_PLIST@"
bootervolumename="EFI"
bootvolume="/Volumes/${bootervolumename}"
install_log="${DEST_VOL}/Private/tmp/Clover_Install_Log.txt"
config_plist_file="${EFI_ROOT_DIR}"/EFI/CLOVER/config.plist
plistbuddy='/usr/libexec/PlistBuddy'

echo "" >> "${install_log}"

# Change config.plist if needed
boottype=$( cat "${DEST_VOL}/boottype" )

case "${boottype}" in
    ia32) perl -i -p -e "s/arch=x86_64/arch=i386/g" "${config_plist_file}" ;;
     x64) perl -i -p -e "s/arch=i386/arch=x86_64/g" "${config_plist_file}" ;;
esac

# Store last install revision
$plistbuddy -c "Add CloverRevision integer %CLOVERREVISION%" "$CLOVER_INSTALLER_PLIST_NEW" >/dev/null

# Get theme from config.plist
config_theme=''
if [[ -f "$config_plist_file" ]]; then
    config_theme=$( $plistbuddy -c "Print :GUI:Theme" "$config_plist_file" 2>/dev/null )
fi

# Check if theme is found
theme_found=0
themes_installed=()

# Store installed themes
$plistbuddy -c "Add InstalledThemes array" "$CLOVER_INSTALLER_PLIST_NEW" >/dev/null
index=0
while read -r -u3 theme_plist; do
    theme=${theme_plist%/*}
    theme=${theme##*/}
    $plistbuddy -c "Add InstalledThemes:$index string $theme" "$CLOVER_INSTALLER_PLIST_NEW" >/dev/null
    [[ -n "config_theme" && "$config_theme" == "$theme" ]] && theme_found=1
    themes_installed+=("$theme")
    index=$((index + 1))
done 3< <(find "${EFI_ROOT_DIR}"/EFI/CLOVER/themes \( -iname 'theme.plist' -or -iname 'theme.svg' \) 2>/dev/null)

# Replace theme if theme directory not found
if [[ "$theme_found" -eq 0 ]]; then
    [[ -n "$config_theme" ]] && echo "Theme '$config_theme' (defined in config.plist) not found !" >> "${install_log}"
    default_theme=${themes_installed[0]}
    if [[ -n "$default_theme" ]]; then
        echo "Using default theme '$default_theme'" >> "${install_log}"
        $plistbuddy -c "Delete :GUI:Theme" "$config_plist_file" &>/dev/null
        $plistbuddy -c "Add :GUI:Theme string $default_theme" "$config_plist_file" 2>&1 >> "${install_log}"
    fi
fi

echo "======================================================" >> "${install_log}"
echo "=========== Clover EFI Installation Finish ===========" >> "${install_log}"
echo "======================================================" >> "${install_log}"

# ---------------------------------------------
# Cleanup
# ---------------------------------------------

if [[ -e "$install_log" && -d "${EFI_ROOT_DIR}/EFI" ]]; then
    mv -f "$install_log" "${EFI_ROOT_DIR}/EFI/Clover_Install_Log.txt"
fi

if [[ -f "$CLOVER_INSTALLER_PLIST_NEW" ]]; then
    mv -f "$CLOVER_INSTALLER_PLIST_NEW" "$CLOVER_INSTALLER_PLIST"
fi

# Clean up
rm -f "${DEST_VOL}/Private/tmp/boottype"

# Remove link to the partition that contain the EFI folder
rm -rf "${DEST_VOL}"/Private/tmp/EFIROOTDIR

# Remove Clover files
rm -rf "${DEST_VOL}"/Private/tmp/usr

# show the log as html immediately
# cp -R "${EFI_ROOT_DIR}/EFI/Clover_Install_Log.txt" /tmp/
# rm -f /tmp/Clover_Install_Log.html
# textutil -cat html -output /tmp/Clover_Install_Log.html /tmp/Clover_Install_Log.txt
# open /tmp/Clover_Install_Log.html



